const User = require("../../model/user.model");
const Role = require("../../model/role.model");
const UserRole = require("../../model/user_role.model");
const Sequelize = require("sequelize");
class UserService {
  async createUser(args) {
    const respnse = await User.create(args);
    return respnse.dataValues;
  }
  async getUserInfo({ id, phone_number, user_type, user_level }) {
    const whereOpt = {};
    id && Object.assign(whereOpt, { id });
    phone_number && Object.assign(whereOpt, { phone_number });
    user_type && Object.assign(whereOpt, { user_type });
    user_level && Object.assign(whereOpt, { user_level });
    const respnse = await User.findOne({
      where: whereOpt,
    });
    return respnse ? respnse.dataValues : null;
  }
  // async updateUserInfo({id,phone_number,password,user_type}) {
  //   const whereOpt = { id };
  //   const newUserInfo = {};
  //   phone_number && Object.assign(newUserInfo, { phone_number });
  //   password && Object.assign(newUserInfo, { password });
  //   user_type && Object.assign(newUserInfo, { user_type });
  //   const respnse = await User.update(newUserInfo, { where: whereOpt });
  //   // 0 表示新密码与旧密码相同 1 表示不同
  //   return respnse[0] > 0 ? true : false;
  // }
  async updateUserInfo(args) {
    const { id, role_ids=[], userRoleList=[], ...remainingParameters } = args;
    // 删除用户角色关联数据
    await UserRole.destroy({
      where: {
        user_id: id,
      },
    });
    // 新增用户角色关联表数据
    const user_role = await UserRole.bulkCreate(
      role_ids.map((item) => {
        return {
          user_id: id,
          role_id: item,
        };
      })
    );
    const resource = await User.update(
      {
        ...remainingParameters,
      },
      {
        where: {
          id,
        },
      }
    );
    return resource;
  }
  async getUserList(args) {
    const { current = 1, pageSize = 10, ...remainingParameters } = args;
    const offset = (current - 1) * pageSize;
    const whereOptions = {};
    if (remainingParameters.nickname) {
      whereOptions.nickname = {
        [Sequelize.Op.like]: `%${remainingParameters.nickname}%`,
      };
    }
    if (remainingParameters.phone_number) {
      whereOptions.phone_number = {
        [Sequelize.Op.like]: `%${remainingParameters.phone_number}%`,
      };
    }
    // 获取用户列表
    const { rows: list, count: total } = await User.findAndCountAll({
      limit: Number(pageSize),
      offset: offset,
      attributes: { exclude: ["password"] },
      where: whereOptions,
      order: [["createdAt", "DESC"]],
    });
    // 获取用户角色关联表数据
    list.forEach(async (item) => {
      item.dataValues.role_ids = [];
      item.dataValues.role_ids = await UserRole.findAll({
        where: { user_id: item.id },
        order: [["createdAt", "DESC"]],
      });
      // 获取角色id
      item.dataValues.role_ids = item.dataValues.role_ids.map(
        (item) => item.role_id
      );
    });
    // 获取角色列表
    const userRoleList = await Role.findAll({
      order: [["createdAt", "DESC"]],
    });
    list.forEach((item) => {
      if (item.dataValues.role_ids.length > 0) {
        // 遍历角色id
        item.dataValues.userRoleList = item.dataValues.role_ids.map(
          (children) => {
            // 筛选出对应角色
            return userRoleList.find((item2) => item2.id === children);
          }
        );
      }
    });
    return {
      current,
      pageSize,
      total,
      list,
    };
  }
}

module.exports = new UserService();
